//
//  AppDelegate.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/15.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabBarViewCtrl.h"
#import "GuideViewCtrl.h"
#import "LoginViewCtrl.h"
#import "LoginManager.h"
#import "PortalInitor.h"

@interface AppDelegate ()

@property (nonatomic,assign)BOOL isShowGuideView;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.isShowGuideView = YES;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self startDisplayInterface];

    [self.window makeKeyAndVisible];
    [self autoLogin];
    //先这样测试
    [[PortalInitor getInstance] loginSuccess];
    
    [NSNotificationCenter addSwitchLogoutStatusNF:self action:@selector(logout:)];

    // Override point for customization after application launch.
    return YES;
}


- (void)autoLogin {
    bool is_login=[kUserDefaults boolForKey:IS_LOGIN];
    if (is_login) {//曾经登录过
        NSString *email = [kUserDefaults objectForKey:LOGIN_EmailAddress];
        NSString *password = [kUserDefaults objectForKey:LOGIN_PASSWord];
        [[LoginManager sharedInstance] loginDictorany:@{LOGIN_EmailAddress:email,LOGIN_PASSWord:password} success:^(UserMode *user) {
            NSLog(@"启动程序登录成功");
        } failed:^(RequestError *error) {
            NSLog(@"启动程序登录失败");
        }];
    }

}

- (void)startDisplayInterface {
    if (self.isShowGuideView) {
        self.window.rootViewController = [[GuideViewCtrl alloc] initWithNibName:@"GuideViewCtrl" bundle:nil];
    } else {
        [self goToMainOrLoginController];
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
{
    // 停止下载所有图片
    [[SDWebImageManager sharedManager] cancelAll];
    // 清除内存中的图片
    [[SDWebImageManager sharedManager].imageCache clearMemory];
    //清除URl缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
}

#pragma mark - 通知
- (void)logout:(NSNotification *)not {
    LoginViewCtrl *logCtrl = [[LoginViewCtrl alloc] initWithNibName:@"LoginViewCtrl" bundle:nil];
    [self.window.rootViewController presentViewController:logCtrl animated:YES completion:nil];
}

#pragma mark - public
// 跳转主界面
- (void)goToMainOrLoginController {
    bool is_login=[kUserDefaults boolForKey:IS_LOGIN];
    MainTabBarViewCtrl *mainCtrl = [[MainTabBarViewCtrl alloc] init];
    self.window.rootViewController = mainCtrl;
    if (!is_login) {
        LoginViewCtrl *logCtrl = [[LoginViewCtrl alloc] initWithNibName:@"LoginViewCtrl" bundle:nil];
        [mainCtrl presentViewController:logCtrl animated:YES completion:nil];
    }
}



@end
